﻿#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using UserManage;
using FrameWorkMain;
using Purchase;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;


#endregion

public partial class Modules_Purchase_ucPurchaseOrderRequest : System.Web.UI.UserControl
{
    #region Fields
    FrameWorkMain.FrameWorkMain frameWork = new FrameWorkMain.FrameWorkMain();
    UserManager _userSearch = new UserManager();
    Purchase.Purchase purchase=new Purchase.Purchase();
    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
       
            txtDate.Text = DateTime.Now.ToShortDateString();
            if (!Page.IsPostBack)
            {
                SetInitialRow();
                //GetMaterials();


            }
           

    }

    protected void ddlItemName_SelectedIndexChanged(object sender, EventArgs e)
    {
////        DropDownList ddl = (DropDownList)sender;
////        int materialId = Convert.ToInt16(ddl.SelectedValue);
////        Response.Write(materialId);
////        materialRate = GetUnitPriceByMaterialId(materialId);
//////        DropDownList ddlItem;
////        TextBox txtRate;
////        foreach (GridViewRow grdRow in gvItemDetails.Rows)
////        {
////            // Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.
////            txtRate = (TextBox)(gvItemDetails.Rows[grdRow.RowIndex].Cells[1].FindControl("txtUnitPrice"));
////            txtRate.Text = materialRate.ToString();


////        }
        //DropDownList ddl = (DropDownList)e.Row.sFindControl("ddlItemName");

       // TextBox txt = (TextBox)((GridView)sender).FindControl("txt");

    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }
   
    protected void gvItemDetails_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnSearchItem_Click(object sender, EventArgs e)
    {
        Response.Write("editt");
    }
    //protected void gvItemDetails_RowDeleted(object sender, GridViewDeletedEventArgs e)
    //{

    //}
    //protected void gvItemDetails_RowCommand(object sender, GridViewCommandEventArgs e)
    //{
    //    PurchaseRequestTableAdapter purchaseRequest = new PurchaseRequestTableAdapter();
    //    bool flag = false;
    //    if (e.CommandName.Equals("Insert"))
    //    {
    //        DateTime date = Convert.ToDateTime(txtDate.Text);
    //         TextBox txtSerialNo = (TextBox)gvItemDetails.FooterRow.FindControl("txtSerialNo");
    //         TextBox txtItemName = (TextBox)gvItemDetails.FooterRow.FindControl("txtItemName");
    //         TextBox txtQuantity = (TextBox)gvItemDetails.FooterRow.FindControl("txtQuantity");
    //         purchaseRequest.Insert(Convert.ToInt16(txtItemName.Text), Convert.ToInt16(txtQuantity.Text), 0,date,date,"", "", 1, 1, "", "");


    //         DataTable dt = new DataTable();
    //         dt.Columns.Add("S.No");
    //         dt.Columns.Add("Item Name");
    //         dt.Columns.Add("Quantity");
    //         DataRow dr = dt.NewRow();
    //         dr["S.No"] = txtSerialNo.Text.ToString();
    //         dr["Item Name"] = txtItemName.Text.ToString();
    //         dr["Quantity"] = txtQuantity.Text.ToString();
    //         dt.Rows.Add(dt);
    //         if (dt.Rows.Count > 0)
    //         {

    //             gvItemDetails.Rows[0].Cells[0].Text = txtQuantity.Text;
    //             gvItemDetails.DataSource = dt.DefaultView;
    //             gvItemDetails.DataBind();
    //         }
    //         else
    //         {
    //             //Call the method for populating your GridView with Dummy Data
    //             BindDummy();
    //         }

    //    //    CheckBox chkNewActive = (CheckBox)grdContact.FooterRow.FindControl("chkNewActive");
    //    //    DropDownList cmbNewType = (DropDownList)grdContact.FooterRow.FindControl("cmbNewType");
    //    //    DropDownList ddlNewSex = (DropDownList)grdContact.FooterRow.FindControl("ddlNewSex");
    //    //    if (chkNewActive.Checked) flag = true; else flag = false;
    //    //    contact.Insert(txtNewName.Text, ddlNewSex.SelectedValue, cmbNewType.SelectedValue, flag);
    //    //    FillGrid();
    //    }
    //}

    //void BindDummy()
    //{

    //   // DataSet inboxDS = new DataSet();
    //  //  inboxDS = frameWork.GetInboxEntries();
    //    DataTable dt = new DataTable();
    //    dt.Columns.Add("S.No");
    //    dt.Columns.Add("Item Name");
    //    dt.Columns.Add("Quantity");
    //    DataRow dr = dt.NewRow();
    //    dr["S.No"] = "1";
    //    dr["Item Name"] = "0";
    //    dr["Quantity"] = "0";
    //    dt.Rows.Add(dr);

    //    gvItemDetails.DataSource = dt;
    //    gvItemDetails.DataBind();

       
    //        //DataTable test = new DataTable();
    //        //test.Columns.Add("Name");
    //        //test.Columns.Add("Description");

    //        //test.Rows.Add(new object[] { "", "" });

    //        //gvItemDetails.DataSource = test;
    //        //gvItemDetails.DataBind(); 




    //}
    #endregion

    #region Methods
    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Column1"] = string.Empty;
        dr["Column2"] = string.Empty;
        dr["Column3"] = string.Empty;
        dr["Column4"] = string.Empty;
        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        gvItemDetails.DataSource = dt;
        gvItemDetails.DataBind();

       // DropDownList ddl1 = (DropDownList)gvItemDetails.Rows[0].Cells[1].FindControl("ddlItemName");
        //GetMaterials();

    }

    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                   // DropDownList box1 = (DropDownList)gvItemDetails.Rows[rowIndex].Cells[1].FindControl("ddlItemName");
                    TextBox box1 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[2].FindControl("txtProduct");
                    TextBox box2 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
                    TextBox box3 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[3].FindControl("txtUnitPrice");
                    TextBox box4 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[3].FindControl("txtTP");

                    
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
                    dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
                  //  itemIndex = Convert.ToInt16(box1.SelectedValue);
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                gvItemDetails.DataSource = dtCurrentTable;
                gvItemDetails.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }

    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                GetMaterials();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                   // DropDownList box1 = (DropDownList)gvItemDetails.Rows[rowIndex].Cells[1].FindControl("ddlItemName");
                    TextBox box1 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[2].FindControl("txtProduct");
                    TextBox box2 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[2].FindControl("txtQuantity");
                    TextBox box3 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[3].FindControl("txtUnitPrice");
                    TextBox box4 = (TextBox)gvItemDetails.Rows[rowIndex].Cells[3].FindControl("txtTP");

                    if (i < dt.Rows.Count - 1)
                    {
                     //   box1.ClearSelection();
                     //   box1.Items.FindByText(dt.Rows[i]["Column1"].ToString()).Selected = true;
                        box1.Text = dt.Rows[i]["Column1"].ToString();
                        box2.Text = dt.Rows[i]["Column2"].ToString();
                        box3.Text = dt.Rows[i]["Column3"].ToString();
                        box4.Text = dt.Rows[i]["Column4"].ToString();
                    }
                 

                    rowIndex++;
                }
            }
        }
    }

    private void GetMaterials()
    {
        DataSet materialsDS = purchase.GetMaterials();
        // DropDownList Control Object Created to bind the data dynamically with each nested DropDownlist control placed inside the template column of the GridView Control.

        // foreach loop is used to loop through each row of GridView Control.
        foreach (GridViewRow grdRow in gvItemDetails.Rows)
        {
            // Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.
            //drdList = (DropDownList)(gvItemDetails.Rows[grdRow.RowIndex].Cells[1].FindControl("ddlItemName"));

            //// DataBinding of nested DropDownList Control for each row of GridView Control.
            //drdList.DataSource = materialsDS;
            //drdList.DataValueField = "ML_MaterialID";
            //drdList.DataTextField = "ML_Name";
            //drdList.DataBind();
        }
    }

    private double GetUnitPriceByMaterialId(int materialId)
    {
        double unitPrice = purchase.GetUnitPriceByMaterialId(materialId);
        return unitPrice;
    }

    #endregion
    protected void gvItemDetails_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        //TextBox txtRate;
        //TextBox txtQuantity;
        //DropDownList ddlItem;
        //foreach (GridViewRow grdRow in gvItemDetails.Rows)
        //{
        //    // Nested DropDownList Control reference is passed to the DrdList object. This will allow you access the properties of dropdownlist placed inside the GridView Template column.
        //    txtRate = (TextBox)(gvItemDetails.Rows[grdRow.RowIndex].Cells[1].FindControl("txtUnitPrice"));
        //    txtRate.Text = materialRate.ToString();

        //    txtQuantity = (TextBox)(gvItemDetails.Rows[grdRow.RowIndex].Cells[1].FindControl("txtQuantity"));
        //    txtQuantity.Text = "1";

        //    //ddlItem=(DropDownList)(gvItemDetails.Rows[grdRow.RowIndex].Cells[1].FindControl("ddlItemName"));
        //    //ddlItem.SelectedIndex=itemIndex;
        //}
        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");

            e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");

        }
        if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            e.Row.CssClass = "grid-view-header";
        if (e.Row.RowType == DataControlRowType.Header)
            e.Row.CssClass = "grid-view-header";

        //Add CSS class on normal row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Normal)
            e.Row.CssClass = "grid-view-normal";

        //Add CSS class on alternate row.
        if (e.Row.RowType == DataControlRowType.DataRow &&
                  e.Row.RowState == DataControlRowState.Alternate)
            e.Row.CssClass = "grid-view-alternate";
    }
    protected void gvItemDetails_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}
